package com.nosweattechnet.placemap;

import java.util.concurrent.ExecutionException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.nosweattechnet.datatypes.Item;
import com.nosweattechnet.mapmyway.R;
import com.nosweattechnet.server.AddItemAsyncTask;

//Activity in charge of getting name and description of new Item
public class AddItemInformationActivity extends Activity {

	private float x;
	private float y;
	private float z;
	private String placeid;
	private String userid="";
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Sets layout to that specified in xml, and retrieves the button on the layout
        setContentView(R.layout.activity_add_item_information);
        Button b=(Button) findViewById(R.id.submitItemButton);
        
        //Gets the data passed in the intent
        Intent intent=getIntent();
		//Gets extended data from the intent
        x=intent.getFloatExtra(getString(R.string.x_coord_intent),0);
        y=intent.getFloatExtra(getString(R.string.y_coord_intent),0);
        z=intent.getFloatExtra(getString(R.string.z_coord_intent),1);
        placeid=intent.getStringExtra(getString(R.string.place_id_intent));
        
        //Sets the behavior of the button
    	b.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				//Gets the entered information then calls the function to add the item
				EditText itemNameText=(EditText) findViewById(R.id.itemName);
				EditText itemDescripText=(EditText) findViewById(R.id.itemDescription);
				String itemName=itemNameText.getText().toString();
				String itemDescrip=itemDescripText.getText().toString();
				addItem(itemName,itemDescrip);
			}
		});
    }
    
	//Adds the item with the specified name and descriptoin, and the placeid, x,y,y and z from the data passed to it
    private void addItem(String itemName,String itemDescrip){
    	AddItemAsyncTask aiat=new AddItemAsyncTask(itemName, itemDescrip, placeid, x, y, z, userid);
    	aiat.execute();
    	try {
			//Get item from server
    		Item i=aiat.getItem();
			DisplayItems.addItem(i);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
    	setContentView(new View(this));
    	//Exits this activity
    	finish();
    }

	//Standard options menu creation
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_add_item_information, menu);
        return true;
    }

    //Selection of item on standard options menu
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
